package com.bluedot.monitor.impl;

import com.bluedot.message.Message;
import com.bluedot.monitor.Monitor;
import com.bluedot.queue.OutQueue;
import com.bluedot.queue.impl.MapperServiceQueue;

/**
 * 监听mapperServiceQueue中消息的monitor
 * 在监听到队列有新消息后通过message携带的信息unpark tomcat线程池中被park的线程
 */
public class MapperServiceQueueMonitor extends Monitor {

    private final OutQueue queue;

    /**
     * 初始化要监听的队列
     */
    private MapperServiceQueueMonitor() {
        queue = MapperServiceQueue.getInstance();
    }

    /**
     * monitorThreadPool线程池中运行的task
     */
    @Override
    public void run() {
        while (true) {
            Message message = queue.peek();
            Monitor.unparkTomcatThread(message.getKey());
        }
    }

    public static MapperServiceQueueMonitor getInstance() {
        return MonitorHolder.mapperServiceQueueMonitor;
    }

    private static class MonitorHolder {

        private static final MapperServiceQueueMonitor mapperServiceQueueMonitor = new MapperServiceQueueMonitor();
    }
}
